package com.kylin.module.member.orm;

import com.kylin.hiberbate.BaseHibernateDAO;
import java.util.Date;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

/**
 * Data access object (DAO) for domain model class Member.
 * @see com.kylin.module.member.orm.Member
 * @author liangzh
 */
public class MemberDAO extends BaseHibernateDAO {

    private static final Log log = LogFactory.getLog(MemberDAO.class);

//	//property constants
////	public static final String USERNAME = "username";
////	public static final String PASSWORD = "password";
////	public static final String HITS = "hits";
////	public static final String LANGUAGE = "language";
//
    
    public void save(Member transientInstance) {
        log.debug("?????? Member instance");
        super.save(transientInstance);
    }
    
	public void delete(Member persistentInstance) {
        log.debug("deleting Member instance");
        super.delete(persistentInstance);

    }
    
    public Member findById( java.lang.Integer id) {
        log.debug("getting Member instance with id: " + id);
        return (Member)findById(Member.class , id);
    }
    
    public List find() {
        log.debug("finding Member instance by example");
        try {
            List results = getSession()
                    .createCriteria("com.kylin.module.member.orm.Member").list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    
    public List findByExample(Member instance) {
        log.debug("finding Member instance by example");
        try {
            List results = getSession()
                    .createCriteria("com.kylin.module.member.orm.Member")
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding Member instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from Member as model where model." 
         						+ propertyName + "= ?";
         Query queryObject = getSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

//	public List findByUsername(Object username) {
//		return findByProperty(USERNAME, username);
//	}
	
//	public List findByPassword(Object password) {
//		return findByProperty(PASSWORD, password);
//	}
	
//	public List findByHits(Object hits) {
//		return findByProperty(HITS, hits);
//	}
	
//	public List findByLanguage(Object language) {
//		return findByProperty(LANGUAGE, language);
//	}
	
    public Member merge(Member detachedInstance) {
        log.debug("merging Member instance");
        try {
            Member result = (Member) getSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(Member instance) {
        log.debug("attaching dirty Member instance");
        try {
            getSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(Member instance) {
        log.debug("attaching clean Member instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
}